//
//  PeopleProfileViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 8/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PeopleProfileViewController.h"
#import "AppDelegate.h"
#import "JGoAPI.h"
#import "User.h"
#import "Request.h"
#import "PeopleDetailViewController.h"
#import "MeetUpViewController.h"
#import "ChatMessageViewController.h"

@implementation PeopleProfileViewController
@synthesize bgFullProfileView;
@synthesize user_info,txtView;

-(void)initNavigationBar 
{  
    [self setBackButtonImageActiveName:nil 
                          inactiveName:@"back-button" 
                                target:self action:@selector(backPressed)];    
}

-(IBAction)meetupPressed:(id)sender
{
    MeetUpViewController *muVC = [[MeetUpViewController alloc] initWithNibName:@"MeetUpViewController" bundle:nil];
    [muVC setPeopleInfo:user_info];
    [self.navigationController pushViewController:muVC animated:YES];
    [muVC release];
    
}
-(IBAction)profilePressed:(id)sender
{
    PeopleDetailViewController *mpVC = [[PeopleDetailViewController alloc] initWithNibName:@"PeopleDetailViewController" bundle:nil];
    [mpVC setPeopleInfo:user_info];
    [self.navigationController pushViewController:mpVC animated:YES];
    [mpVC release];

}
-(void) backPressed 
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)loadData:(MBProgressHUD*)progress
{ 
    NSString *uCCMString = nil;
    NSString *dotString = @"\u00B7";
    NSString *university = [user_info objectForKey:@"university"];
    NSString *course = [user_info objectForKey:@"course"];
    NSString *uClass = [user_info objectForKey:@"u_year"];
    NSArray *modules = [user_info objectForKey:@"modules"];
    NSDictionary *settings = [user_info objectForKey:@"setting"];
    
    if (![[settings objectForKey:@"course"]integerValue]) {
        uCCMString = [NSString stringWithFormat:@"%@",university];
    }else {
        uCCMString = [NSString stringWithFormat:@"%@%@%@",university,dotString,course];
    }
    if (![[settings objectForKey:@"u_year"]integerValue]) {
        
    }else {
        uCCMString = [NSString stringWithFormat:@"%@%@%@",uCCMString,dotString,uClass];
    }
    if (![[settings objectForKey:@"modules"]integerValue]) {
        
    }else {
        for (NSString *aString in modules) {
            if (![aString isEqualToString:@""]) {
                uCCMString = [NSString stringWithFormat:@"%@%@%@",uCCMString,dotString,aString];
            }
            
        }
    }
    
    
    
    txtView.text = uCCMString;

    [progress hide:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    bgFullProfileView.backgroundColor = [UIColor colorWithRed:18./225 green:40./225 blue:74./225 alpha:0.8];
    [self initNavigationBar];
    //NSLog(@"%@",user_info);
    [self setTitle:[user_info objectForKey:@"name"]];
    [self performSelector:@selector(loadData:) withObject:nil];
    [backgroundAvatar setImage:[user_info objectForKey:@"avatar"]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

-(void) dealloc
{
    [user_info release];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)addFav:(MBProgressHUD*)progress 
{
    NSDictionary *result = nil;
    NSError *error = nil;
    NSDictionary *userInfo = [JGoUtility sharedCreateAccount];
    
    result = [JGoAPI addFavoriteWithUserId:[userInfo objectForKey:@"user_id"] 
                                  peopleId:[user_info objectForKey:@"user_id"] 
                                     error:&error];
    
    if (error) {
        [JGoUtility showAlert:[error localizedDescription]];
    }
    else {
        NSLog(@"%@",result);        
        [JGoUtility showAlert:@"Sucessfully"];                
    }
    
    [progress hide:YES];
}
-(IBAction)addFavPress:(id)sender
{
    [JGoUtility showProgressInView:self.view 
                            target:self 
                             title:@"Adding..." 
                          selector:@selector(addFav:)];

    
}

-(IBAction)message:(id)sender
{
    [JGoUtility showProgressInView:[JGoUtility frontFromView:self.view]
                            target:self
                             title:@"New Message..."
                          selector:@selector(delayAddConversation:)];
}

- (void)delayAddConversation:(MBProgressHUD*)progress
{
    NSString *userId = [[JGoUtility sharedCreateAccount] objectForKey:@"user_id"];
    NSError *error = nil;
    NSString *inviteeIds = [user_info objectForKey:@"user_id"];
    NSString *textMessage = @"Message Request";
    NSDictionary *conver = [JGoAPI createConversationWithUserId:userId
                                               conversationName:textMessage
                                                     inviteeIds:inviteeIds
                                                          error:&error];
    if (!error) {
        NSString *conversionId = [[conver objectForKey:@"data"] objectForKey:@"id"];
        
        /*
        BOOL result = [JGoAPI addMessageWithUserId:userId
                                    conversationId:conversionId
                                              type:@"0"
                                           content:content
                                             error:&error];
        */
        
        if (YES) {
            
            ChatMessageViewController *cmVC = [[[ChatMessageViewController alloc] initWithNibName:@"ChatMessageViewController"
                                                                                           bundle:nil] autorelease];
            cmVC.conversionId = conversionId;
            
            [self.navigationController pushViewController:cmVC animated:YES];
            
        }        
    }
    else {
        [JGoUtility showAlert:[error localizedDescription]];
    }
    
    [progress hide:YES];
}

@end
